import axios from 'axios'
import Qs from 'qs'
import {BASE_URL, ERR_OK} from './config'

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
axios.defaults.withCredentials = true

let http = axios.create({
  baseURL: BASE_URL.api,
  transformRequest: [function (params) {
    // let params = new URLSearchParams(data);
    params = Qs.stringify(params)
    return params
  }]
})

/* 过滤请求 */
http.interceptors.request.use((config) => {
  return config
})
/* 过滤响应 */
http.interceptors.response.use((result) => {
  /* 假设当code为0时代表响应成功 */
  if (result.data.error != ERR_OK) {
    return Promise.reject(result)
  }
  return result.data
}, result => {
  return Promise.reject(result)
})

export default http
